import { request } from '../../profile';


export const composeUrl = (url) => {
  if (url.startsWith('http://') || url.startsWith('https://')) {
    return url;
  }
  return `${request}/${url}`;
};

const checkStatus = (response) => {
  if (response.status >= 200 && response.status < 300) {
    return response.json();
  }
  return new Promise((res, rej) => {
    response.json().then((json) => {
      rej(json);
    });
  });
};

export const post = (url, data) => {
  return fetch(composeUrl(url), {
    method: 'post',
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(data),
  }).then(checkStatus);
};
export const get = (url) => {
  return fetch(composeUrl(url), {
    credentials: 'include',
    method: 'get',
  })
    .then(checkStatus);
};
